package com.example.myretrofit2.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myretrofit2.Car;
import com.example.myretrofit2.R;
import com.example.myretrofit2.activity.OnitemClickListener;
import com.example.myretrofit2.activity.RecyclerItemClickListener;
import com.example.myretrofit2.adapter.CarAdapter;
import com.example.myretrofit2.common.MyApplication;
import com.example.myretrofit2.presenter.CarPresenter;
import com.example.myretrofit2.presenter.MyPresenter;
import com.example.myretrofit2.presenter.MySubscriber;
import com.google.gson.internal.bind.util.ISO8601Utils;

import java.util.List;


public class CarFragment extends Fragment {
RecyclerView recyclerView;
TextView total;
float count;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
         View view = inflater.inflate(R.layout.fragment_car, container, false);
//        fm = getActivity().getSupportFragmentManager();
//        FragmentTransaction ft = fm.beginTransaction();
//        Car2Fragment car2Fragment = new Car2Fragment();
//        ft.add(R.id.car_fram,car2Fragment).commit();
        count=0;
       initView(view);
       initData();
        return view;
    }



    public void initView(View view){
        total = view.findViewById(R.id.total);
        recyclerView = view.findViewById(R.id.goods_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MyApplication.getContext());
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
    }
    public void initData(){
        CarPresenter.queryCar(new MySubscriber<List<Car>>(MyApplication.getContext()) {
            @Override
            public void onNext(final List<Car> cars) {
                for (Car car:cars){
                    count+=(car.getNum()*car.getPrice());
                }
                sum(count,total);
                CarAdapter carAdapter = new CarAdapter(MyApplication.getContext(),cars,count,total);
                recyclerView.setAdapter(carAdapter);

                recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getContext(), recyclerView, new OnitemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                    }

                    @Override
                    public void onLongItemClick(View view, final int position) {
                        final int id = cars.get(position).getId();
                        new AlertDialog.Builder(getContext())
                                .setTitle("删除购物车")
                                .setMessage("你确定要删除这种商品吗？")
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        CarPresenter.delCar(new MySubscriber<Boolean>(MyApplication.getContext()) {
                                            @Override
                                            public void onNext(Boolean aBoolean) {
                                                if(aBoolean){
                                                    Toast.makeText(getContext(),"删除成功",Toast.LENGTH_SHORT).show();
                                                    count = count - cars.get(position).getPrice()*cars.get(position).getNum();
                                                    sum(count,total);
                                                    cars.remove(position);
                                                    recyclerView.setAdapter(new CarAdapter(MyApplication.getContext(),cars,count,total));
                                                }
                                            }
                                        },id);
                                    }
                                })
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        dialogInterface.dismiss();
                                    }
                                }).create().show();
                    }
                }));
            }
        });
    }

    public void sum(float count,TextView textView){
        textView.setText("总价："+count);
    }
}